<template>
    <div>
      <el-form :inline="true">
        <el-form-item>
          <el-input clearable v-model="query.keyWord" placeholder="学员姓名/模式名称" style="width: 100%"></el-input>
        </el-form-item>

        <el-form-item>
          <el-select clearable filterable v-model="query.grade_type" placeholder="请选择年级">
            <el-option
              v-for="item in gradeTypeList"
              :key="item.id"
              :label="item.value"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-select
            v-if="query.grade_type"
            clearable filterable
            v-model="query.subjectId"
            placeholder="请选择科目">
            <el-option
              v-for="item in subjectList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button
            v-if="hasPermission(['system:study:list'])"
            type="primary" @click="searchStudyHistory" icon="el-icon-search">查询</el-button>
        </el-form-item>

        <el-form-item>
          <el-button
            v-if="hasPermission(['system:study:exportStudyHistory'])"
            @click="exportStudyHistory" icon="el-icon-upload2" type="success">导出</el-button>
        </el-form-item>
      </el-form>

      <el-table
        :data="studyHistoryList"
        border
        v-loading="loading"
        style="width: 100%;">

        <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="50">
        </el-table-column>

        <el-table-column
          prop="name"
          align="center"
          label="学员姓名">
        </el-table-column>

        <el-table-column
          prop="name"
          align="center"
          label="年级">
          <template slot-scope="scope">
            <el-tag size="small">{{scope.row.grade_type | getGradeName}}</el-tag>
          </template>
        </el-table-column>

        <el-table-column
          prop="mode_name"
          align="center"
          label="模式名称">
        </el-table-column>

        <el-table-column
          prop="subject_name"
          align="center"
          label="科目">
        </el-table-column>

        <el-table-column
          prop="start_date"
          align="center"
          :formatter="formatterStartDate"
          width="180"
          label="开始时间">
        </el-table-column>

        <el-table-column
          prop="end_date"
          align="center"
          :formatter="formatterEndDate"
          width="180"
          label="结束时间">
        </el-table-column>

        <el-table-column
          prop="study_time"
          align="center"
          width="180"
          label="学习时间">
        </el-table-column>
      </el-table>

      <el-pagination
        background
        @size-change="changeSize"
        @current-change="changePage"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        :total="totalCount"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </div>
</template>

<script>
  import {getGradeType} from '@/utils/education-api'
  let thisPage = null
  export default {
    name: 'studyHistory',
    data () {
      return {
        loading: true,
        query: {
          keyWord: '',
          grade_type: '',
          subjectId: ''
        },
        exportLoading: null,
        currentPage: 1,
        pageSize: 10,
        totalCount: 0,
        subjectList: [],
        studyHistoryList: [],
        gradeTypeList: []
      }
    },

    watch: {
      'query.grade_type' (val) {
        let that = this
        that.axios.get(that.$http.httpUrl('/system/subject'), {
          params: {
            sqlId: 'system.subject.list',
            gradeType: val
          }
        }).then(function (response) {
          that.subjectList = response.data.data
        }).catch(function (error) {
          console.log(error)
        })
      }
    },

    beforeCreate () {
      thisPage = this
    },

    mounted () {
      let that = this
      that.getStudyHistoryList()
      getGradeType(function (data) {
        that.gradeTypeList = data
      })
    },

    methods: {

      searchStudyHistory () {
        this.currentPage = 1
        this.getStudyHistoryList()
      },

      exportStudyHistory () {
        let that = this
        that.exportLoading = that.$loading({
          lock: true,
          text: '正在导出',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
        that.axios.post(that.$http.httpUrl('/system/study/exportStudyHistory'), {
          keyWord: that.query.keyWord,
          gradeType: that.query.grade_type
        }).then(function (response) {
          if (response.data.code === 1) {
            that.$message.success('导出成功')
            setTimeout(function () {
              location.href = that.$http.getFileHost() + '/' + response.data.message
            }, 500)
          } else {
            that.$message.error('数据导出失败')
          }
          that.exportLoading.close()
        })
      },

      formatterEndDate (row, column) {
        if (!row.end_date) {
          return '--'
        }
        return this.moment(row.end_date).format('YYYY-MM-DD HH:mm')
      },

      formatterStartDate (row, column) {
        if (!row.start_date) {
          return '--'
        }
        return this.moment(row.start_date).format('YYYY-MM-DD HH:mm')
      },

      changeSize (val) {
        this.pageSize = val
        this.currentPage = 1
        this.getStudyHistoryList()
      },

      changePage (val) {
        this.currentPage = val
        this.getStudyHistoryList()
      },

      getStudyHistoryList () {
        let that = this
        that.axios.get(that.$http.httpUrl('/system/study'), {
          params: {
            sqlId: 'study.history.list',
            offset: (that.currentPage - 1) * that.pageSize,
            limit: that.pageSize,
            keyWord: that.query.keyWord,
            subjectId: that.query.subjectId,
            gradeType: that.query.grade_type
          }
        }).then(function (response) {
          that.studyHistoryList = response.data.data
          that.totalCount = response.data.total
          that.loading = false
        })
      }
    },

    filters: {
      getGradeName (gradeType) {
        for (let i = 0; i < thisPage.gradeTypeList.length; i++) {
          if (thisPage.gradeTypeList[i].code === gradeType) {
            return thisPage.gradeTypeList[i].value
          }
        }
      }
    }
  }
</script>
<style scoped>

</style>
